import { deleteFile, lookFile, uploadFile, mergeFile, download, addNote, getOne } from '@/api/file'

const getDefaultState = () => {}

const state = getDefaultState()

const mutations = {}

const actions = {
  //查询文件
  async lookFile({ commit, state }, file) {
    return new Promise((resolve, reject) => {
      lookFile(file)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  //删除文件
  async deleteFile({ commit, state }, ids) {
    return new Promise((resolve, reject) => {
      deleteFile(ids)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  //上传文件
  async uploadFile({ commit, state }, file) {
    return new Promise((resolve, reject) => {
      var form = new FormData()
      form.append('file', file.raw === undefined ? file : file.raw)
      uploadFile(form)
        .then((response) => {
          resolve(response)
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
  //合并文件
  async mergeFile({ commit, state }, formData) {
    return new Promise((resolve, reject) => {
      mergeFile(formData)
        .then((response) => {
          resolve(response)
        })
        .catch((err) => {
          reject(err)
        })
    })
  },

  //新增文件备注
  async addNote({ commit, state }, file) {
    return new Promise((resolve, reject) => {
      addNote(file)
        .then((response) => {
          resolve(response)
        })
        .catch((err) => {
          reject(err)
        })
    })
  },

  //查询一个文件信息
  async getOne({ commit, state }, id) {
    return new Promise((resolve, reject) => {
      getOne(id)
        .then((response) => {
          resolve(response)
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
  //下载文件
  async download({ commit, state }, id) {
    return new Promise((resolve, reject) => {
      download(id)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
